#include <iostream>
#include <fstream>
#include <random>
#include <vector>
#include <string>
#include <cstdint>
#include <cstdlib>

int main(int argc, char* argv[]) {
    if (argc != 3) {
        std::cerr << "用法: " << argv[0] << " <数量> <输出文件路径>\n";
        return 1;
    }

    // 解析命令行参数
    size_t n = std::stoull(argv[1]);
    std::string filepath = argv[2];

    // 设置每次写入的块大小（例如：1MB）
    const size_t chunk_size = 1024 * 1024;
    std::vector<uint8_t> buffer(chunk_size);

    // 初始化随机数生成器
    std::random_device rd;
    std::mt19937_64 rng(rd());
    std::uniform_int_distribution<uint8_t> dist(0, 255);

    // 打开输出文件
    std::ofstream out(filepath, std::ios::binary);
    if (!out) {
        std::cerr << "无法打开输出文件: " << filepath << "\n";
        return 1;
    }

    size_t total_bytes = n * 16;
    size_t bytes_written = 0;
    while (bytes_written < total_bytes) {
        size_t bytes_to_write = std::min(chunk_size, total_bytes - bytes_written);
        for (size_t i = 0; i < bytes_to_write; ++i) {
            buffer[i] = dist(rng);
        }
        out.write(reinterpret_cast<char*>(buffer.data()), bytes_to_write);
        bytes_written += bytes_to_write;
    }

    std::cout << "已生成随机二进制文件: " << filepath << "\n";
    return 0;
}
